/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.invoice.header.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.invoice.header.service.dto.YxfpInvoiceHeaderDTO;
import com.jeeplus.invoice.header.domain.YxfpInvoiceHeader;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 抬头管理MAPPER接口
 * @author 张如利
 * @version 2022-01-20
 */
public interface YxfpInvoiceHeaderMapper extends BaseMapper<YxfpInvoiceHeader> {

    /**
     * 根据id获取抬头管理
     * @param id
     * @return
     */
    YxfpInvoiceHeaderDTO findById(String id);

    /**
     * 获取抬头管理列表
     *
     * @param queryWrapper
     * @return
     */
    IPage <YxfpInvoiceHeaderDTO> findList(Page <YxfpInvoiceHeaderDTO> page, @Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    /**
     * 根据客户ID获取抬头管理列表
     *
     * @param customerId
     * @return
     */
    @Select("SELECT a.* FROM yxfp_invoice_header a WHERE a.customer_id = #{0} ORDER BY a.default_flag DESC")
    List<YxfpInvoiceHeaderDTO> findListByCustomerId(String customerId);

    /**
     * 设置该抬头为默认抬头
     * @param id
     */
    @Update("UPDATE yxfp_invoice_header SET default_flag='1' WHERE id=#{0}")
    void setDefaultInvoiceHeader(String id);

    /**
     * 根据客户ID设置所有抬头为非默认，设置客户默认前先调用该方法
     * @param customerId
     */
    @Update("UPDATE yxfp_invoice_header SET default_flag='0' WHERE customer_id=#{0}")
    void setNotDefalutInvoiceHeaderByCustomerID(String customerId);

    /**
     * 删除抬头
     * @param id
     */
    @Delete("delete from yxfp_invoice_header where id=#{0}")
    void delInvoiceHeaderById(String id);

    /**
     * 根据客户Id获取默认抬头
     * @param customerId
     */
    @Select("SELECT a.* FROM yxfp_invoice_header a WHERE a.customer_id = #{0} and default_flag = '1' ORDER BY a.default_flag DESC")
    YxfpInvoiceHeaderDTO getDefaultHeaderByCustomerId(String customerId);
}
